//
// Created by 15068 on 6/1/2021.
//

#ifdef __linux__

#include <cstdio>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/termios.h>
#include <adt.h>

struct termios original_tio;

void disableInputBuffer()
{
    tcgetattr(STDIN_FILENO, &original_tio);
    struct termios new_tio = original_tio;
    new_tio.c_lflag &= ~ICANON & ~ECHO;
    tcsetattr(STDIN_FILENO, TCSANOW, &new_tio);
}

void restoreInputBuffer()
{
    tcsetattr(STDIN_FILENO, TCSANOW, &original_tio);
}

void handleInterrupt(int signal)
{
    restoreInputBuffer();
    printf("\n");
    exit(-2);
}

UINT16 checkKey()
{
    fd_set readfds;
    FD_ZERO(&readfds);
    FD_SET(STDIN_FILENO, &readfds);

    struct timeval timeout;
    timeout.tv_sec = 0;
    timeout.tv_usec = 0;
    return select(1, &readfds, NULL, NULL, &timeout) != 0;
}

#endif

